# coding:utf-8
import sys
from config import DB_CONFIG
from data.SqlHelper import SqlHelper as SqlHelper

sqlhelper = SqlHelper()
sqlhelper.init_db()



def store_data(queue):
    '''
    读取队列中的数据，写入数据库中
    :param queue2:
    :return:
    '''
    successNum = 0
    failNum = 0
    while True:
        while True:
            try:
                novelInfo = queue.get(timeout=300)
                print("de" + str(novelInfo))
            except BaseException as e:
                print(e)
        # try:
        #     novelInfo = queue.get(timeout=300)
        #     print(novelInfo)
        #     bookName = novelInfo['bookName']
        #     bookAuthor = novelInfo['bookAuthor']
        #     condition = {'bookName': bookName, 'bookAuthor': bookAuthor}
        #
        #     #在这里把抓取的小说放入队列里面
        #     checkBook = sqlhelper.select(conditions=condition)
        #     if len(checkBook) > 0:
        #         #print('更新小说:%s' % (novelInfo['bookName']))
        #         sqlhelper.update(conditions=condition, value=novelInfo)
        #         str = 'HiXiaoShuo----->>>>>>>>update %s' % (bookName)
        #         sys.stdout.write(str + "\r")
        #         sys.stdout.flush()
        #     else:
        #         #print('添加小说:%s' % (novelInfo['bookName']))
        #         sqlhelper.insert(novelInfo)
        #         str = 'HiXiaoShuo----->>>>>>>>Success add %s' % (bookName)
        #         sys.stdout.write(str + "\r")
        #         sys.stdout.flush()
        # except BaseException as e:
        #     str = 'HiXiaoShuo-----xxxxxxxxxxxxxx'
        #     sys.stdout.write(str + "\r")
        #     sys.stdout.flush()
